package com.qiantu.phone.widget.setting;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.getString;

import android.bluetooth.BluetoothClass;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.activity.BrowserActivity;
import com.qiantu.phone.ui.activity.UniversalConfigActvity;


public class DeviceSettingCardConfig extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;

    public DeviceSettingCardConfig(AppActivity context, DeviceBean deviceBean) {
        this(context, null, deviceBean);
    }

    public DeviceSettingCardConfig(AppActivity context, AttributeSet attrs, DeviceBean deviceBean) {
        this(context, attrs, 0, deviceBean);
    }

    public DeviceSettingCardConfig(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean);
    }

    public DeviceSettingCardConfig(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(R.string.card_config);
        setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        if(mDevice.getDeviceType().equals("universal_ac")){
            Intent intent = new Intent(getContext(),UniversalConfigActvity.class);
            intent.putExtra("title",getString(R.string.card_config));
            intent.putExtra("deviceSerialNo",mDevice.getDeviceSerialNo());
            getContext().startActivity(intent);
        }else{
            String deviceControl = "";
            if(mDevice.getDeviceType().equals("universal_freshair")){
                deviceControl = "";
            }else if(mDevice.getDeviceType().equals("universal_sequencer")){
                deviceControl = "sequenceCardConfig";
            }else if(mDevice.getDeviceType().equals("universal_projector")){
                deviceControl = "projectorCardConfig";
            }else if(mDevice.getDeviceType().equals("universal_player")){
                deviceControl = "playCardConfig";
            }else if(mDevice.getDeviceType().equals("universal_pa")){
                deviceControl = "powerAmplifierCardConfig";
            }
            int androidHeight = DensityUtils.getStatusBarHeight(mContext) + getResources().getDimensionPixelSize(R.dimen.dp_16);
            StringBuilder stringBuilder = new StringBuilder(BuildConfig.DEVICE_URL);

//            StringBuilder stringBuilder = new StringBuilder("http://192.168.2.65:8080/");
            stringBuilder.append(deviceControl).append("/?token=").append(AppApplication.getInstance().getLoginInfo().getToken())
                    .append("&houseId=").append(AppApplication.getInstance().getCurrentHouseSerialNo())
                    .append("&deviceSerialNo=").append(mDevice.getDeviceSerialNo())
                    .append("&androidHeight=").append("'" + androidHeight + "'")
                    .append("&phoneType=").append("android")
                    .append("&phone=").append(AppApplication.getInstance().getLoginInfo().getUserInfo().getPhone());
            BrowserActivity.start(getContext(), stringBuilder.toString());
        }
    }

}
